<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class GameController extends GlobalController
{
    //列表
    public function tclist()
    {
        $pageSize = 20;
        $where = array();
        $user_id = I('get.user_id', 0, 'intval');
        if (!empty($user_id)) {
            $where['user_id'] = $user_id;
        }
        $this->assign('user_id', $user_id == 0 ? '' : $user_id);
        $egg_id = I('get.egg_id', 0, 'intval');
        if (!empty($egg_id)) {
            $where['egg_id'] = $egg_id;
        }
        $this->assign('egg_id', $egg_id == 0 ? '' : $egg_id);
        $recordModel = M('game_tc');
        $count = $recordModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $recordModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $userModel = M('user');
        $eggModel = M('egg');
        foreach ($list as $key => $row) {
            $userInfo = $userModel->where(array('user_id' => $row['user_id']))
                ->find();
            $list[$key]['nickname'] = $userInfo['nickname'];
            $eggInfo = $eggModel->field('egg_name')
                ->where(array('egg_id' => $row['egg_id']))
                ->find();
            $list[$key]['egg_name'] = $eggInfo['egg_name'];
        }
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    public function tylist()
    {
        $pageSize = 20;
        $where = array();
        $user_id = I('get.user_id', 0, 'intval');
        if (!empty($user_id)) {
            $where['user_id'] = $user_id;
        }
        $this->assign('user_id', $user_id == 0 ? '' : $user_id);
        $egg_id = I('get.egg_id', 0, 'intval');
        if (!empty($egg_id)) {
            $where['egg_id'] = $egg_id;
        }
        $this->assign('egg_id', $egg_id == 0 ? '' : $egg_id);
        $recordModel = M('game_ty');
        $count = $recordModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $recordModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $userModel = M('user');
        $eggModel = M('egg');
        foreach ($list as $key => $row) {
            $userInfo = $userModel->where(array('user_id' => $row['user_id']))
                ->find();
            $list[$key]['nickname'] = $userInfo['nickname'];
            $eggInfo = $eggModel->field('egg_name')
                ->where(array('egg_id' => $row['egg_id']))
                ->find();
            $list[$key]['egg_name'] = $eggInfo['egg_name'];
        }
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    public function publish()
    {
        if (IS_POST) {
            $id = I('post.id', '', 'intval');
            $table = I('post.table', 'game_tc', 'trim');
            $recordModel = M($table);
            $recordModel->where(array('id' => $id))->setField('status', array('exp', '1 - status'));
            $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
        }
    }

    public function del()
    {
        $id = I('post.id', 0, 'intval');
        $table = I('post.table', 'game_tc', 'trim');
        $recordModel = M($table);
        $recordModel->where(array('id' => $id))->delete();
        $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
    }
}